﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz.QuanTriHeThong;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms.QuanTriHeThong
{
    public partial class ViewNguoiSuDung : Form
    {
        public ViewNguoiSuDung()
        {
            InitializeComponent();
        }

        private void SetPreferences()
        {
            radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.ShowGroupPanel = false;
            radGridView1.MasterTemplate.EnableGrouping = false;
            radGridView1.EnableHotTracking = true;
        }

        private void BuildData()
        {
            radGridView1.MasterTemplate.AllowAddNewRow = false; 
            radGridView1.MasterTemplate.AutoGenerateColumns = false;

            var biz = new NguoiSuDungBiz();
            var tem = biz.ViewAll();
            radGridView1.MasterTemplate.DataSource = tem;

            if (tem.Count == 0)
            {
                buttonEdit.Enabled = false;
                buttonRemove.Enabled = false;
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.QuanTri);
                parent.Status.Text = string.Empty;
            }

            SetPreferences();
            BuildData();
        }

        private void Edit(object sender, EventArgs e)
        {
            var dataRowInfo = this.radGridView1.CurrentRow as GridViewDataRowInfo;
            if (dataRowInfo != null)
            {
                var id = (int) dataRowInfo.Cells[5].Value;

                var editForm = new NewNguoiSuDung(id);
                Utilities.CloseForm(this, editForm);
            }
        }

        private void NewUser(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new NewNguoiSuDung());
        }

        private void Remove(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                var items = new List<NguoiSuDung>();

                foreach (var row in radGridView1.Rows)
                {
                    if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
                    {
                        items.Add(row.DataBoundItem as NguoiSuDung);
                    }
                }

                var biz = new NguoiSuDungBiz();
                biz.RemoveItem(items);
            }

            Utilities.CloseForm(this, new ViewNguoiSuDung());
        }
    }
}
